package com.sk.sourcecircle.activity.find.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.find.act.ContactActivity;
import com.sk.sourcecircle.activity.find.act.RoundFriendActivity;
import com.sk.sourcecircle.activity.home.activity.GroupDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.KindActivity;
import com.sk.sourcecircle.activity.home.activity.RoundGroupActivity;
import com.sk.sourcecircle.activity.login.LoginActivity;
import com.sk.sourcecircle.adapter.GroupListAdapter;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.callback.MyCallBack;
import com.sk.sourcecircle.dialog.TipDialogFragment;
import com.sk.sourcecircle.entity.GroupListBean;
import com.sk.sourcecircle.entity.HomeTabList;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.utils.SnackUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/11/28.
 */

public class RoungGroupFragment extends BaseFragment implements HandleDataCallBack, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.search)
    TextView search;
    @BindView(R.id.round_friend)
    TextView roundFriend;
    @BindView(R.id.round_group)
    TextView roundGroup;
    @BindView(R.id.contact)
    TextView contact;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.more_list)
    TextView moreList;
    @BindView(R.id.more_group)
    RelativeLayout moreGroup;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;
    private GroupListAdapter adapter;
    private List<GroupListBean.DataBean> allDataList = new ArrayList<>();
    private LoadingDialog loadingDialog;
    private int pos;
    private boolean isRefresh = false;
    private MyCallBack myCallBack;

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_round_group;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        swipe.setColorSchemeResources(R.color.home_checked_color, R.color.home_checked_color);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doRefresh();
            }
        });


        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new GroupListAdapter(R.layout.item_group, allDataList);
        dataRyc.setAdapter(adapter);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);

        AppApi.getGroupList(null, MyApplication.getInstance().getLng(), MyApplication.getInstance().getLat(), 1, this, 1, getActivity());
    }

    @OnClick({R.id.search, R.id.round_friend, R.id.round_group, R.id.contact, R.id.more_group})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search:
                Intent intent3 = new Intent(mContext, ContactActivity.class);
                startActivity(intent3);
                break;
            case R.id.round_friend:
                Intent intent = new Intent(mContext, RoundFriendActivity.class);
                startActivity(intent);
                break;
            case R.id.round_group:
                Intent intent1 = new Intent(mContext, RoundGroupActivity.class);
                startActivity(intent1);
                break;
            case R.id.contact:
                Intent intent2 = new Intent(mContext, ContactActivity.class);
                startActivity(intent2);
                break;
            case R.id.more_group:
                AppApi.getTabList(RoungGroupFragment.this, 0, getActivity());
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                HomeTabList homeTabList = JSON.parseObject(result, HomeTabList.class);
                ArrayList<HomeTabList.DataBean> dataBeanList = (ArrayList<HomeTabList.DataBean>) homeTabList.getData();
                Intent menuIntent = new Intent(mContext, KindActivity.class);
                menuIntent.putParcelableArrayListExtra("data", dataBeanList);
                startActivity(menuIntent);
                break;
            case 1:
                if (isRefresh) {
                    isRefresh = false;
                    swipe.setRefreshing(false);
                    SnackUtils.show(mRootView, "刷新成功！");
                }
                GroupListBean groupListBean = JSON.parseObject(result, GroupListBean.class);
                List<GroupListBean.DataBean> dataBeans = groupListBean.getData();
                allDataList.clear();
                allDataList.addAll(dataBeans);
                adapter.notifyDataSetChanged();
                break;
            case 2:
                Toast.makeText(mContext, "关注成功！", Toast.LENGTH_SHORT).show();
                allDataList.get(pos).setFollow(1);
                adapter.notifyItemChanged(pos);
                break;
            case 3:
                Toast.makeText(mContext, "取关成功！", Toast.LENGTH_SHORT).show();
                allDataList.get(pos).setFollow(0);
                adapter.notifyItemChanged(pos);
                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.item_bg:
                Intent intent = new Intent(mContext, GroupDetailsActivity.class);
                intent.putExtra("id", allDataList.get(position).getCommunity_id());
                startActivity(intent);
                break;
            case R.id.add_care:
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(intent1, 300);
                    return;
                }

                pos = position;
                loadingDialog.show();
                AppApi.addCareGroup(allDataList.get(position).getCommunity_id(), RoungGroupFragment.this, 2, getActivity());
                break;
            case R.id.cancel_care:
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent intent2 = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(intent2, 300);
                    return;
                }
                pos = position;
                TipDialogFragment tipDialogFragment = new TipDialogFragment();
                tipDialogFragment.setContent("确认取消关注此社群？");
                tipDialogFragment.setMyCallBack(RoungGroupFragment.this);
                tipDialogFragment.show(getFragmentManager(), "tip");
                break;
        }
    }

    @Override
    public void sure() {
        super.sure();
        loadingDialog.show();
        AppApi.cancelCareGroup(allDataList.get(pos).getCommunity_id(), RoungGroupFragment.this, 3, getActivity());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            if (requestCode == 300) {
                loadingDialog.show();
                AppApi.getGroupList(null, MyApplication.getInstance().getLng(), MyApplication.getInstance().getLat(), 1, this, 1, getActivity());
            }
        }
    }

    public void doRefresh() {
        swipe.setRefreshing(true);
        isRefresh = true;
        AppApi.getGroupList(null, MyApplication.getInstance().getLng(), MyApplication.getInstance().getLat(), 1, this, 1, getActivity());
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, GroupDetailsActivity.class);
        intent.putExtra("id", allDataList.get(position).getCommunity_id());
        startActivity(intent);
    }
}
